/**
 * 
 */
package com.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.database.TransactionManager;
import com.model.EmpresaBean;

/**
 * @author Isac
 *
 */
public class EmpresaDao {

	private String TABLE_EMPRESA = "empresa";
	private String COLN_ID       = "emp_id";
	private String COLN_RAZAO    = "emp_razao";
	private String COLN_FANTASIA = "emp_fantasia";
	private String COLN_CNPJ     = "emp_cnpj";
	
	public static String createTableEmpresa(){
		StringBuilder sb = new StringBuilder();
		
		sb.append(" create table empresa (                      ");
		sb.append("   emp_id integer primary key autoincrement, ");
		sb.append("   emp_razao text,                           ");
		sb.append("   emp_fantasia text,                        ");
		sb.append("   emp_cnpj text);                           ");
		
		return sb.toString();
	}
	
	public void insert(TransactionManager transaction, EmpresaBean empBean) {
		ContentValues values = new ContentValues();
		values.put(COLN_ID, empBean.getId());
		values.put(COLN_RAZAO, empBean.getRazao());
		values.put(COLN_FANTASIA, empBean.getFantasia());
		values.put(COLN_CNPJ, empBean.getFantasia());
		transaction.prepare().insert(TABLE_EMPRESA, null, values);
	}
	
	public boolean delete(TransactionManager transaction, EmpresaBean cotId){
		return transaction.prepare().delete(TABLE_EMPRESA,COLN_ID +" = "+cotId, null) > 0 ;
		
	}
	
	public long update(TransactionManager transaction, EmpresaBean empBean ) {
		ContentValues values = new ContentValues();
		values.put(COLN_RAZAO, empBean.getRazao());
		values.put(COLN_FANTASIA, empBean.getFantasia());
		values.put(COLN_CNPJ, empBean.getFantasia());
		return transaction.prepare().update(TABLE_EMPRESA, values,COLN_ID +" = "+empBean.getId(), null);
		
	}
	
	public EmpresaBean openById(TransactionManager transaction, long empId) {
		StringBuilder sql = new StringBuilder();
		sql.append("        select * from                  ");
		sql.append(          TABLE_EMPRESA                  );
		sql.append("         where "+COLN_ID+" = "+ empId   );
		EmpresaBean empBean = new EmpresaBean();
		Cursor cursor = transaction.prepare().rawQuery(sql.toString(), null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst(); 
			empBean.setId(cursor.getInt(cursor.getColumnIndex(COLN_ID)));
			empBean.setRazao(cursor.getString(cursor.getColumnIndex(COLN_RAZAO)));
			empBean.setFantasia(cursor.getString(cursor.getColumnIndex(COLN_FANTASIA)));
			empBean.setCnpj(cursor.getString(cursor.getColumnIndex(COLN_CNPJ)));
			cursor.close();
			return empBean;
		} else {
			cursor.close();
			return null;
		}		
	}
	
	public List<EmpresaBean> getAll(TransactionManager transaction) {
		StringBuilder sql = new StringBuilder();
		sql.append("        select * from                  ");
		sql.append(          TABLE_EMPRESA                  );
		List<EmpresaBean> cotList = new ArrayList<EmpresaBean>();
		Cursor cursor = transaction.prepare().rawQuery(sql.toString(), null);
		while (cursor.moveToNext()) {
			EmpresaBean empBean = new EmpresaBean();
			empBean.setId(cursor.getInt(cursor.getColumnIndex(COLN_ID)));
			empBean.setRazao(cursor.getString(cursor.getColumnIndex(COLN_RAZAO)));
			empBean.setFantasia(cursor.getString(cursor.getColumnIndex(COLN_FANTASIA)));
			empBean.setCnpj(cursor.getString(cursor.getColumnIndex(COLN_CNPJ)));
			cotList.add(empBean);
		} 
			cursor.close();
			return cotList;
	}
}
